var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
      listData:[],
      formData :{},
      lists:[{}],
  },
  methods: {
    //新增
    addList: function(){
      var  lists = this.data.lists;
      var newData = {};
      lists.push(newData);//实质是添加lists数组内容，使for循环多一次
      this.setData({
        lists: lists,
      })
    },
    //删除
    delList: function () {
      let lists = this.data.lists;
      let formDatas = this.data.formData;
      if(lists.length !== 1)
      {
        lists.pop();      //实质是删除lists数组内容，使for循环少一次
        if(formDatas.length>1)
        {
          formDatas.pop();
        }
        this.setData({
          lists: lists,
          formData: formDatas,
        })
      }
    },
    //input存值
    formInputChange(e) {
        let index = e.target.dataset.field
        let count = index.charAt(index.length-1);
        let name = index.substring(0,index.length-1)
        this.setData({
            [`formData[${[count]}].${name}`]: e.detail.value
        })
    },
    //计算
    inpuCount(e) {
      let index = e.target.dataset.field
      let count = index.charAt(index.length-1);
      this.setData({
        [`formData[${[count]}].perCyceNetMarg`]: this.data.formData[count].perCyceDbusiIncome-this.data.formData[count].perCyceInputCost,
      })
    },
    //提交
    submitForm: function() {
      var obj = {
          plantList: this.data.formData,
          custId: this.data.custId
      }
      console.info(this.data.formData);
      this.setData({
        disabled:true
      });
      wx.request({
          url: baseUrl+"/report/addRptFarmPlantInfoList",
          method: 'POST',
          dataType: 'json',
          data: obj,
          header: {
            'content-type': 'application/json'
          },
          success: function(res) { 
            wx.showToast({
              title: "保存成功",
              icon: 'none',
              duration: 2000,
              success:function(){
                setTimeout(function () {
                  //要延时执行的代码
                  wx.navigateBack({ //跳转到前一个页面
                    delta:1,//前一个页面
                  });
                }, 2000) //延迟时间
              }
            });
            },
            fail: function(res) {
                wx.showToast({
                title: '服务器网络错误,请稍后重试',
                icon: 'loading',
                duration: 1500
                })
            },
            complete: function(res) {
            },
        })
    },
    //查
    onLoad: function(option) {
      var that = this;
      that.setData({
        custId:option.custId,
        [`formData.custId`]: option.custId
      });
      var data = that.data.formData;
      //查询数据回显
      wx.request({
          url: baseUrl+"/report/selectRptFarmPlantInfo",
          method: 'POST',
          data: data,
          header: {
              'content-type': 'application/json'
          },
          dataType: 'json',
          success: function(res) { 
              console.info(res.data.code);
              if(res.data.code == 2)
              {
                wx.showToast({
                  title: res.data.data,
                  icon: 'none',
                  duration: 2000,
                  success:function(){
                    setTimeout(function () {
                      //要延时执行的代码
                      wx.navigateBack({ //跳转到前一个页面
                        delta:1,//前一个页面
                      });
                    }, 2000) //延迟时间
                  }
                });
              }
              else
              {
                let lists = that.data.lists;
                if(res.data.data.length>0){
                  lists.pop();  //清空
                  let newData = {};
                  for (let i = 0; i < res.data.data.length; i++) {
                    lists.push(newData);//放入相同量的数组
                  }
                  that.setData({
                    lists: lists,
                    formData: res.data.data,
                  })
                }
              }
          },
          fail: function(res) {
              wx.showToast({
              title: '服务器网络错误,请稍后重试',
              icon: 'loading',
              duration: 1500
              })
          },
          complete: function(res) {
          },
      })
    },
  }
});